﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Helpers;
using Model;
using System.Collections.Generic;
using System.IO;

namespace FEIStorageSite
{
    public partial class _Default : System.Web.UI.Page
    {
        public const int LAST_FILES_COUNT = 5;
        public List<CategoryItem> categoriesList;
        public int index = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["logout"] != null)
            {
                FormsAuthentication.SignOut();
            }

            DAL.connectionString = "Data Source=" + Server.MapPath("App_Data\\fei_storage.db");

            categoriesList = DAL.GetCategories();
            List<FileItem> filesList;
            if (Request["show_all"] != null)
            {
                filesList = DAL.GetFilesList();
                titleMain.Text = "<h3>Всі завантаження</h3>";
                showAllButton.Visible = false;
            }
            else
            {
                filesList = DAL.GetLastFiles(LAST_FILES_COUNT);
            }
            foreach (FileItem curFileItem in filesList)
            {
                try
                {
                    string filePath = Server.MapPath(curFileItem.Path);
                    FileInfo fileInfo = new FileInfo(filePath);
                    curFileItem.FileSize = fileInfo.Length.ToString();
                }
                catch (Exception exception)
                {
                    curFileItem.FileSize = "0";
                }
            }

            lastFilesList.DataSource = filesList;
            lastFilesList.DataBind();

        }
    }
}
